<script type="text/javascript">
    common.setMainTitle("Quản trị deal"+ "<div>&nbsp;</div>");
</script>

<form id="mainform" name="frm" method="POST" action="/admin/deal/input">
    <div>
        <div id="toolbar-button">
            <div class="item" onclick="add()" id="addButton" title="Add">
                <div id="addnew-global-button"></div>
            </div>
            
            <div class="item" onclick="deleteAll()" id="deleteButton" title="Delete">
                <div id="delete-global-button"></div>
            </div>
            
            <div class="item" onclick="changeStatusMultiItems(1)" id="activateButton" title="Activate">
                <div id="save-global-button"></div>
            </div>
            
            <div class="item" onclick="changeStatusMultiItems(0)" id="deactivateButton" title="Deactivate">
                <div id="back-global-button"></div>
            </div>
        </div>
    </div>
    
    <table style="margin-top: 10px; clear: both;" border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
        <thead>
            <tr>
                <th width="3%" class="table-header-repeat line-left" style="text-align: center">
                    <input type="checkbox" name="DELALL" onclick="selectAll(this,'ITEM')"/>
                </th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Deal ID</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Name</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Store Name</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Download</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Used</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Remaining</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Publish Date</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Expirate Date</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Expirate Hour</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>PAID</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Status</a></th>
                <th class="table-header-repeat line-left" style="text-align: left"><a>Action</a></th>
            </tr>
        </thead>
        <tbody>
            <script type="text/javascript">
                var arrNav = new Array();
            </script>
            <?php
                if(count($this->data) == 0){
                    echo "<tr><td colspan='13'> There is not any data</td></tr>";
                }else{
                    $stt = 1;
                    //echo $this->data; exit;
                    //var_dup($this->data); exit;
                    foreach($this->data as $item){
                        ?>
                        <tr style="cursor: pointer">
                            <!-- <td style="text-align: center"><?=$stt?></td> -->
                            <td style="text-align: center; padding-left: 0px;">
                                <input type="checkbox" name="ITEM[]" value="<?= $item['DEALID']?>"/>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['DEALID'];?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['NAME'];?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['STORENAME']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['HASORDEREDTOTAL']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['USEDTOTAL']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['ORDERTOTAL'] - $item['HASORDEREDTOTAL']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['PUBLISHDATE']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['PUBLISHDATE']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['EXPIRATIONDATE']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['ISPAID']?>    
                                </div>
                            </td>
                            
                            <td>
                                <div>
                                    <?=$item['STATUS']?>    
                                </div>
                            </td>
                            
                            <td style="text-align: center;">
                                <div>
                                    <select id="<?php echo 'actionbox'.$item['DEALID']?>" name="actionbox" onchange="chooseAction(<?=$item['DEALID'] ?>)">
                                        <option value="0">Choosen an Actions</option>
                                        <option value="<?=$item['DEALID']?>">Edit</option>
                                        <option value="<?=$item['DEALID']?>">Publish</option>
                                        <option value="<?=$item['DEALID']?>">Delete</option>
                                        <option value="<?=$item['DEALID']?>">View History Usage</option>
                                    </select>    
                                </div>
                            </td>
                        </tr>
                        <?php
                        $stt++;
                    }
                } 
                
            ?>
        </tbody>
    </table>
    <div id="pagination-div" width="100%">
        <?= $this->pagination?>
    </div>
</form>

<script type="text/javascript">
    
    function chooseAction(dealId){
        text = $("#actionbox"+dealId+ " option:selected").text();
        if(text == 'Edit'){
                window.location = "/admin/deal/input/id/"+dealId + "/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
        }else if(text == "Delete"){
                check = confirm("Do you want to delete this user?");
                if(check){
                        window.location = "/admin/deal/delete/id/" +dealId + "/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
                }
        }else if(text == "Change Password"){
                window.location = "/admin/deal/changepass/id/" +dealId + "/limit/" + '<?= $this->limit?>/page/' + '<?= $this->page?>';
        }
    }
    
    function selectAll(selObj,childObj){
        var arr = $(input[name=childObj+'[]']);
        for(var i = 0; i< arr.length; i++){
            arr[i].checked = selObj.checked
        }
    }
    
    function add(){
        window.location = '/admin/deal/input';
    }
    
    function changeStatusMultiItems(status){
        var ln = 0;
        var j = 0;
        var arr = $(input[name='ITEM[]']);
        var arrValue = new Array();
        for(var i = 0; i < arr.length; i++){
            if(arr[i].checked == true){
                arrValue[j] = arr[i].value;
                j++;
            }
        }
        if(arrValue.length >0){
            var question;
            if(status == 1){
                question = 'Do you want to activate these items?';
            }else{
                question = 'Do you want to inactivate these items?';
            }
            
            if(confirm(question)){
                $.ajax({
                    url:"/admin/deal/changestatusmultiitems",
                    type: "POST",
                    date: {itemIDs: arrValue, status : status},
                    dateType: "text",
                    success: function(data){
                        for(var i = 0; i < arr.length; i++){
                            if(status == 0){
                                $('#status' + arrValue[i]).html('<a href="#" onClick="changeStatus('+ arrValue[i]+ ',1)">Inactive</a>');
                            } else {
                                $('#status' + arrValue[i]).html('<a href="#" onClick="changeStatus('+ arrValue[i]+ ',0)">Active</a>');
                            }
                            arr[i].checked=false;
                        }
                    }
                });
            }
        }else{
            alert("You have to choose at least 1 item!!");
        }
    }
    
    function deleteAll(){
        var ln = 1;
        var arr = $(input[name = 'ITEM[]']);
        for(var i = 0; i< arr.length; i++){
            if(arr[i].checked == true){
                ln = 1;
                break;
            }
        }
        if(ln == 1){
            if(confirm("<?php echo "Do you want to delete these items?"; ?>")){
                document.frm.action="/admin/deal/deletemultiitem";
                document.frm.method="post";
                document.frm.submit();
            }
        }else{
            alert("You have to choose at least 1 item!!");
        }
    }
</script>